package gdma.hqdes.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import common.mybatis.Queries;
import gdma.hqdes.bean.Article;
import gdma.hqdes.bean.ArticleTemplate;
import gdma.hqdes.dao.ArticleMapper;
import gdma.hqdes.dao.ArticleTemplateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Nullable;
import java.util.Date;

/**
 * @Author wangyue
 * @Version 3.0
 * @Date 2024-08-30
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleTemplateMapper articleTemplateMapper;

    @Autowired
    ArticleMapper articleMapper;

    @Override
    public ArticleTemplate getTemplate(Integer templateId) {
        return articleTemplateMapper.selectById(templateId);
    }

    @Override
    public Page<ArticleTemplate> listTemplates(@Nullable String keyword, int... pagination) {
        LambdaQueryWrapper<ArticleTemplate> criteria = new QueryWrapper<ArticleTemplate>().lambda();
        if(StringUtils.hasText(keyword)) criteria.like(ArticleTemplate::getName, keyword);
        criteria.orderByDesc(ArticleTemplate::getCreatedTime);
        return Queries.paginate(() -> articleTemplateMapper.selectList(criteria), pagination);
    }

    @Override
    public ArticleTemplate saveTemplate(ArticleTemplate template) {
        if(template.getId() != null) {
            template.setUpdatedTime(new Date());
            articleTemplateMapper.updateById(template);
        } else {
            template.setCreatedTime(new Date());
            articleTemplateMapper.insert(template);
        }
        return template;
    }

    @Override
    public Article getArticle(Long id) {
        return articleMapper.selectById(id);
    }

    @Override
    public Page<Article> listArticles(@Nullable Integer templateId, @Nullable Long projectId, @Nullable Long unitId, @Nullable String keyword, int... pagination) {
        LambdaQueryWrapper<Article> criteria = new QueryWrapper<Article>().lambda();
        if(templateId != null) criteria.eq(Article::getTemplateId, templateId);
        if(projectId != null) criteria.eq(Article::getProjectId, projectId);
        if(unitId != null) criteria.eq(Article::getUnitId, unitId);
        if(StringUtils.hasText(keyword)) criteria.like(Article::getName, keyword);
        criteria.orderByDesc(Article::getCreatedTime);
        return Queries.paginate(() -> articleMapper.selectList(criteria), pagination);
    }

    @Override
    public Article saveArticle(Article article) {
        if(article.getId() != null) {
            articleMapper.updateById(article);
        } else {
            articleMapper.insert(article);
        }
        return article;
    }

    @Override
    public boolean deleteArticle(Long id) {
        return articleMapper.deleteById(id) > 0;
    }

}
